package com.zjitc.web.controller;

import com.zjitc.pojo.User;
import com.zjitc.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserSerivce userSerivce;

    @RequestMapping("register")
    public String register(User user,Model model, HttpSession session){
        String msg = userSerivce.register(user);
        if (msg.equals("")){
            session.setAttribute("msg","注册成功请登录");
            return "/user/login";
        }else {
            model.addAttribute("msg",msg);
            return "/user/register";
        }

    }

    @RequestMapping("login")
    public String login(User user, Model model, HttpSession session){
        String msg = userSerivce.login(user);
        if (msg.equals("")){
            session.setAttribute("user",user);
            return "redirect:/shop/home.do";
        }else {
            model.addAttribute("msg",msg);
            return "/user/login";
        }
    }

    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.setAttribute("user",null);
        return "redirect:/shop/home.do";
    }
}
